<template>
  <div class="OfflineContract">
    <van-steps :active="active" active-color="#38f">
      <van-step>申请</van-step>
      <van-step>图集选用</van-step>
      <van-step>签订合同</van-step>
      <van-step>四到场</van-step>
    </van-steps>
    <inputSelect :isShowCity="false" :contentData.sync="OfflineContract" :JFData.sync="JFData" :title="''" @getValues="getValues" />
    <div v-if="rowData?.OPTYPE == '60'">
      <select-picker
        :bitian="true"
        :isTime="false"
        :ISrequired="isreadonly"
        :labels="'项目负责人'"
        :values="'nickname'"
        :DICID="'idCard'"
        :columns="projectData"
        :selectValue="XXHT_XMFZR"
        @getMessage="setValue"
        placeholder="请选择项目负责人"></select-picker>
    </div>
    <div v-if="rowData?.OPTYPE == '60'">
      <select-picker
        :bitian="true"
        :isTime="false"
        :ISrequired="isreadonly"
        :labels="'施工负责人'"
        :values="'nickname'"
        :DICID="'idCard'"
        :columns="projectDataXM"
        :selectValue="XXHT_SGFZR"
        @getMessage="setValue1"
        placeholder="请选择施工负责人"></select-picker>
    </div>
    <upLoad
      @getFileServerUpload1="getFileID1"
      @getFileServerUpload="getFileID"
      @delectFile="delectFiless"
      :rowData="rowData"
      :FJData="file"
      :isReadOnly="false"
      :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'"
      :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
    <div class="HT_footer">
      <van-button v-if="fileListID.length == 0" style="width: 90vw" @click="getUpadatefun" type="info">提交</van-button>
      <van-button v-else style="width: 90vw" @click="getUpadatefun" type="info">确认无误</van-button>
    </div>
  </div>
</template>

<script>
import inputSelect from '../NewZzd/public/inputSelect.vue'
// import SelectCity from '../public/SelectCity.vue'
//eca的文件上传组件 可上传 删除 预览pdf
import upLoad from '@/utils/upLoad'
import selectPicker from '@/utils/selectPicker'
import { GSA_Add, GSA_Search1 } from '@/common/commonApi'
import { houseRemoldUnitBuildPersonSG, houseRemoldUnitProjectPersonXM, houseRemoldUnitBuildPersonXM, QuerySignCallback } from '../../utils/api'
import { GJDBFunction2 } from '@/utils/queryData'
export default {
  name: 'OfflineContract',
  data() {
    return {
      active: 2,
      OfflineContract: [
        {
          label: '签订时间',
          key: 'HTQDRQ',
          ISinput: false,
          required: true,
          readonly: true,
          isTime: true
        },
        {
          label: '合同金额(万元)',
          key: 'HTJE', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        },
        {
          label: '建筑面积(m²)',
          key: 'HTJZMJ', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        },
        {
          label: '房屋高度(m)',
          key: 'HTFWCG', //绑定的字段
          ISinput: true, //true:输入框
          required: true, //true: 必填项
          readonly: true, //true:可以填写 false：不可以填写
          isTime: false //true:可以时间选择器 false：下拉框
        }
      ],
      fileListID: [],
      //项目负责人
      isXM: false,
      isSG: false,
      isreadonly: false,
      projectDataXM: [],
      projectData: [],
      XXHT_XMFZR: '',
      XXHT_SGFZR: '',
      JFData: [
        {
          ROW_GUID: '',
          TO_ROW_GUID: '',
          PKID: '',
          HTQDRQ: '',
          HTJE: '',
          HTJZMJ: '',
          HTFWCG: '',
          XXHT_XMFZR: '',
          XXHT_SGFZR: ''
        }
      ],
      file: [
        {
          title: '合同文件',
          FileName: [],
          groupGuid: '2774cbb5-1854-4a0a-afaf-bee2e8f83cab',
          fileList: [],
          isReadOnly: true
        }
      ]
    }
  },
  props: {
    rowData: Object
  },
  methods: {
    getValues(name, value, label) {
      console.log(name, value, label, this.JFData)
    },
    setValue(name, value) {
      console.log(name, value, 'wseeddd')
      //请求
      this.JFData.XXHT_XMFZR = name
      this.getProgectData1(value)
    },
    setValue1(name, value) {
      console.log(name, value)
      //请求
      this.JFData.XXHT_SGFZR = name
    },
    //根据当前的角色确认合同更新对应的字段  农户、代办人：HTBHQDZT   工匠：HTQDZT  项目负责人：F_JZQYXMFZR 施工负责人：F_JZQYSGFZR
    async getUpadateQUE() {
      let params = {
        uscc: this.rowData.TYSH,
        idCard: window.localStorage.getItem('DanDianid')
      }
      const res = await houseRemoldUnitBuildPersonXM(params)
      //如果是1 为项目负责人 如果为2 施工负责人 如果为1,2
      console.log(this.isSG, 'this.isSG')
      if (res.data.data[0] == 1) {
        this.isXM = true
      } else if (res.data.data[0] == 2) {
        this.isSG = true
        if (this.rowData.HTBHQDZT == null && this.rowData.HTQDZT == null && this.rowData.F_JZQYXMFZR == null && this.rowData.F_JZQYSGFZR == null) {
          this.isreadonly = true
          this.$confirm({
            title: '提示',
            message: '施工负责人不支持上传合同',
            btn: {
              okText: '确定',
              cancelText: '取消'
            }
          })
            .then(() => {
              // do something
            })
            .catch(() => {
              console.log('no')
            })
        }
      } else if ((res.data.data[0] == 1, 2)) {
        this.isSG = true
        this.isXM = true
      } else {
        this.isSG = false
      }
      console.log(this.isSG, 'this.isSG')
    },
    //获取施工负责人
    async getProgectData1(data) {
      let params = {
        isDelete: 0,
        uscc: this.rowData.TYSH,
        projectPersonIdcard: data
      }
      const res = await houseRemoldUnitBuildPersonSG(params)
      this.projectDataXM = res.data.data
      if (this.JFData.XXHT_SGFZR) {
        this.projectDataXM.forEach((item) => {
          console.log(item.idCard, this.JFData.XXHT_SGFZR, 'knini')
          if (item.idCard == this.JFData.XXHT_SGFZR) {
            this.XXHT_SGFZR = item.nickname
          }
        })
      }
    },
    //获取项目负责的人
    async getProgectData() {
      let params = {
        isDelete: 0,
        uscc: this.rowData.TYSH
      }
      const res = await houseRemoldUnitProjectPersonXM(params)
      this.projectData = res.data.data
      if (this.JFData.XXHT_XMFZR) {
        this.projectData.forEach((item) => {
          if (item.idCard == this.JFData.XXHT_XMFZR) {
            this.XXHT_XMFZR = item.nickname
          }
        })
        this.getProgectData1(this.JFData.XXHT_XMFZR)
      }
    },
    delectFiless(data) {
      if (data) {
        this.fileListID = ''
      }
    },
    async getUpadatefun() {
      // 06424a16-746c-4650-ae97-a774402de7cd
      if (this.rowData.HTBHQDZT == null && this.rowData.HTQDZT == null && this.rowData.F_JZQYXMFZR == null && this.rowData.F_JZQYSGFZR == null && this.isSG && !this.isXM) {
        this.$toast.fail('请联系农户或者项目负责人上传合同')
        return false
      }
      if (this.fileListID.length == 0) {
        this.$toast.fail('请先上传合同')
        return
      }
      if (this.$store.state.isGJ && window.localStorage.getItem('userType') != 'QY') {
        this.JFData[0].HTBHQDZT = '1'
      } else if (!this.$store.state.isGJ && window.localStorage.getItem('userType') != 'QY') {
        this.JFData[0].HTQDZT = '1'
      } else if (window.localStorage.getItem('userType') == 'QY' && this.isXM) {
        //需要判断当前人是项目负责人还是施工负责人
        this.JFData[0].F_JZQYXMFZR = '1'
      } else if (window.localStorage.getItem('userType') == 'QY' && this.isSG) {
        this.JFData[0].F_JZQYSGFZR = '1'
      } else if (window.localStorage.getItem('userType') == 'QY' && this.isSG && this.isXM) {
        this.JFData[0].F_JZQYSGFZR = '1'
        this.JFData[0].F_JZQYXMFZR = '1'
      }
      const res = await GSA_Add('06424a16-746c-4650-ae97-a774402de7cd', false, ...this.JFData)
      if (this.$store.state.VERSIONNUMBER == 1) {
        this.$toast.success('更新成功')
        this.$router.push({
          path: '/ZLJIndex'
        })
      } else {
        this.$router.push({
          path: '/ZLJIndex'
        })
      }
    },
    //回显
    getFileID1(data) {
      this.fileListID = data.fileList.length > 0 ? data.fileList : data.FileName
    },
    //上传之后
    async getFileID(data) {
      //需要用存储过程把id存下来
      let params = [
        {
          i_fileName: data[0].FILENAME,
          i_to_row_guid: this.rowData.ROW_GUID,
          i_fileGuid: data[0].FILEGUID,
          i_type: 1
        }
      ]
      const res = await GJDBFunction2('update_fileGuid_by_fileName_and_toRowGuid', params)
      this.fileListID = data
      console.log(this.fileListID, 'djhfbue')
    },
    //获取线下签合同数据
    async getData1111() {
      let data = [{ field: 'ROW_GUID', value: this.rowData.ROW_GUID }]
      const res = await GSA_Search1('06424a16-746c-4650-ae97-a774402de7cd', data)
      console.log(res, 'edkifnuierobfguijorefguijrbnfuji')
      for (let i in this.JFData[0]) {
        for (let j in res.Data[0]) {
          if (i == j) {
            this.JFData[0][i] = res.Data[0][j]
          }
        }
      }
    }
  },
  created() {
    console.log(this.$router.query, 'this.$router.query')
    this.getProgectData()
    this.getData1111()
    if (window.localStorage.getItem('userType') == 'QY') {
      this.getUpadateQUE()
    }
  },
  components: {
    inputSelect,
    upLoad,
    selectPicker
  }
}
</script>

<style scoped lang="scss">
.OfflineContract {
  width: 100%;
  //   height: 100vh;
  background: #fff;
  overflow-y: auto;
  padding: 10px 6px;
  .HT_footer {
    display: flex;
    /* height: 201px; */
    /* align-items: center; */
    justify-content: space-around;
    // margin-top: 5%;
  }
}
</style>
